//
//  LHQDestinationPhotoViewController.m
//  God_is_a_Girl
//
//  Created by dllo on 15/9/24.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LHQDestinationPhotoViewController.h"
#import "LHQDestinationPhotoModel.h"
#import "LHQDestinationphotoCollectionViewCell.h"
#import <AFNetworking.h>
#import "LHQDestinationDetailViewController.h"
#define WIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define HEIGH ([UIScreen mainScreen].bounds.size.height/667)
@interface LHQDestinationPhotoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,retain) NSMutableArray  *arr;

@property (nonatomic,retain) UITableView  *tableView;

@property (nonatomic,retain) UICollectionViewFlowLayout  *flowLayout;

@property (nonatomic,retain) UICollectionView  *collection;



@end

@implementation LHQDestinationPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.count = self.number;
    
    if (self.count == 3) {
        [self dataHandel2];
    }
    if (self.count == 6) {
        [self dataHandel5];
    }
    if(self.count == 8) {
       [self dataHandel6];
    }
//    NSLog(@"%ld",self.number);
}
-(void)collectionViewSubView
{
    //创建瀑布流
    self.flowLayout = [[UICollectionViewFlowLayout  alloc] init];
    //创建对象
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:self.flowLayout];
    //方块大小
    self.flowLayout.itemSize = CGSizeMake(335*WIDTH /2, 150);
    //最小列间距
    self.flowLayout.minimumInteritemSpacing = 10 ;
    //最小行间距
    self.flowLayout.minimumLineSpacing = 10 ;
    //设置item据四边的间距
    self.flowLayout.sectionInset = UIEdgeInsetsMake(15 , 15 , 15 , 15);
    [self.view  addSubview:self.collection];
    self.collection.backgroundColor = [UIColor whiteColor];
    
    //两个协议
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    //collectionViewcell 必须提前注册
    [self.collection registerClass:[LHQDestinationphotoCollectionViewCell  class] forCellWithReuseIdentifier:@"reuse"];
    
    
}

-(void)dataHandel2//欧洲国家
{
    self.arr  = [NSMutableArray array];
    
    AFHTTPRequestOperationManager  *manager = [AFHTTPRequestOperationManager   manager];
    
    [ manager  GET:@"http://api.breadtrip.com/destination/index_places/3/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.arr = [responseObject objectForKey:@"data"];//图片和名字
        
        [self.collection  reloadData];
        [self collectionViewSubView];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

-(void)dataHandel5//亚洲国家
{
    self.arr  = [NSMutableArray array];
    
    AFHTTPRequestOperationManager  *manager = [AFHTTPRequestOperationManager   manager];
    
    [ manager  GET:@"http://api.breadtrip.com/destination/index_places/6/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.arr = [responseObject objectForKey:@"data"];//图片和名字
        [self.collection  reloadData];
        [self collectionViewSubView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
-(void)dataHandel6//国内国家
{
    self.arr  = [NSMutableArray array];
    
    AFHTTPRequestOperationManager  *manager = [AFHTTPRequestOperationManager   manager];
    
    [ manager  GET:@"http://api.breadtrip.com/destination/index_places/8/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.arr = [responseObject objectForKey:@"data"];//图片和名字
        
        [self.collection  reloadData];
        
        [self collectionViewSubView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count ;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.count == 2) {
        
        NSDictionary  *dic = [self.arr objectAtIndex:indexPath.row];
        
        LHQDestinationPhotoModel  *model = [[LHQDestinationPhotoModel  alloc] init];
        [model  setValuesForKeysWithDictionary:dic];
        
        LHQDestinationphotoCollectionViewCell  *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    else if (self.count == 5) {
        
        NSDictionary  *dic = [self.arr objectAtIndex:indexPath.row];
        
        LHQDestinationPhotoModel  *model = [[LHQDestinationPhotoModel  alloc] init];
        [model  setValuesForKeysWithDictionary:dic];
        
        LHQDestinationphotoCollectionViewCell  *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    
    else {
        
        NSDictionary  *dic = [self.arr objectAtIndex:indexPath.row];
       
        LHQDestinationPhotoModel  *model = [[LHQDestinationPhotoModel  alloc] init];
        
        [model  setValuesForKeysWithDictionary:dic];
        
        LHQDestinationphotoCollectionViewCell  *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        cell.model = model;
        
        return cell;
    }
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHQDestinationDetailViewController  *detail = [[LHQDestinationDetailViewController  alloc] init];
    
    NSString *idStr = [[self.arr objectAtIndex:indexPath.item ]objectForKey:@"id"];
    detail.detailStr = idStr;
    detail.type = [[self.arr objectAtIndex:indexPath.item ]objectForKey:@"type"];
   [self.navigationController pushViewController:detail animated:NO];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
